<script>
    import { http } from './api';
    import store from './store';
    export default {
        onLaunch: function (option) {
            var me = this;

            const updateManager = uni.getUpdateManager();
            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                // console.log(res);
            });
            updateManager.onUpdateReady(function () {
                uni.showModal({
                    title: '更新提示',
                    content: '新版本已经准备好，请立即重启应用',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                            updateManager.applyUpdate();
                        }
                    }
                });
            });
            updateManager.onUpdateFailed(function (res) {
                // 新版本下载失败
                console.log(res);
            });
            /**
             * 初次加载判断网络情况
             * 无网络状态下根据实际情况进行调整
             */
            uni.getNetworkType({
                success(res) {
                    const networkType = res.networkType;

                    if (networkType === 'none') {
                        me.globalData.isConnected = false;
                        uni.showToast({
                            title: '当前无网络',
                            icon: 'loading',
                            duration: 5000,
                            complete: function () {
                                setTimeout(function () {
                                    console.log('getNetworkType跳转到/404');
                                    uni.redirectTo({
                                        url: '/pages/404/404'
                                    });
                                }, 1000);
                            }
                        });
                    }
                }
            });
            /**
             * 监听网络状态变化
             * 可根据业务需求进行调整
             */

            uni.onNetworkStatusChange(function (res) {
                if (!res.isConnected) {
                    me.globalData.isConnected = false;
                    uni.showToast({
                        title: '网络已断开',
                        icon: 'loading'
                    });
                    setTimeout(function () {
                        console.log('onNetworkStatusChange跳转到/404');
                        uni.redirectTo({
                            url: '/pages/404/404'
                        });
                    }, 1000);
                } else {
                    me.globalData.isConnected = true;
                    uni.hideToast();
                }
            });

            //获取信息 开始

            this.getToken();
        },
        globalData: {
            supermarketId: '1642802375047573505', //超市ID

            selectProduct: [], //已选的商品

            userId: undefined, //用户ID

            userMianInfo: {}, //用户主表信息

            userInfo: {}, //用户微信表信息

            villageInfo: {}, //小区信息

            //头像
            avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',

            //未注册的信息 openId 和 sessionKey
            wxCodeInfo: {},

            //如果是推荐的 推荐人id
            recommeId: undefined,

            //赠送的商品信息
            freeGoodsList: {}
        },
        onShow: function () {
            uni.setStorageSync('loginTwo', false);
        },
        mounted() {
            // console.log('111111111111111111111111111111111111111',this.$store.getters.selectProduct)
            //uni.setStorageSync("token", 'eyJhbGciOiJIUzI1NiJ9.eyJkYXRhS2V5Ijoie1widG9rZW5UeXBlXCI6XCJDVEVfUENfUExBVEZPUk1cIixcInRva2VuVHlwZUVudW1cIjpcIlRPS0VOX1RZUEVfRU5VTV9CQUNLXCIsXCJ1c2VySWRcIjpcIjFcIn0iLCJuYmYiOjE2NzgxNTIxNzgsImV4cCI6MTY3ODQxMTM3OCwiaWF0IjoxNjc4MTUyMTc4fQ.WZTPirIetwcbdUDqTuTx2K2efPyHoy-qrSHF5-Bd9u8')
        },
        methods: {
            //Appid
            Appid() {
                return wx.getAccountInfoSync().miniProgram.appId;
            },
            //获取小程序名称
            getAppName() {
                return typeof __wxConfig == 'object' ? __wxConfig.accountInfo.nickname : '';
            },
            //后请求
            laterToken(cb, time) {
                time || (time = 500);

                if (this.globalData.userId) {
                    cb(true);

                    return;
                }

                setTimeout(() => {
                    this.getToken().then(bool => {
                        cb(bool);
                    });
                }, time);
            },
            //获取登录Token  bool：代表是否强制更新
            getToken(bool) {
                let that = this;

                return new Promise((resolve, reject) => {
                    // resolve(false)
                    // return

                    //如果是有用户信息了  直接返回
                    if (this.globalData.userId && !bool) {
                        resolve(true);

                        return;
                    }

                    // console.log('没有信息么',this.globalData.wxCodeInfo)

                    //如果有openId  代表已经请求过了的
                    if (this.globalData.wxCodeInfo.openId && !bool) {
                        resolve(!!this.globalData.userId);

                        return;
                    }

                    wx.login({
                        success: res => {
                            // 发送 res.code 到后台换取 openId, sessionKey,
                            //此处可以请求后台接口获取openid

                            console.log('登陆接口111111111111111111111111111111111111111', res);

                            http(`flash/smallapplet/login?code=${res.code}&t=${new Date().getTime()}`).then(res => {
                                // console.log("登陆接口", res)

                                if (res.code == 200) {
                                    let token = res.data.token;

                                    //判断是否是已注册用户
                                    if (token) {
                                        this.getCurrentUser(res.data);

                                        resolve(true);

                                        return;
                                    }
                                    // console.log("未注册", res)

                                    this.globalData.wxCodeInfo = res.data;
                                    //未注册用户
                                    resolve(false);
                                } else {
                                    wx.showToast({
                                        title: res.message
                                    });
                                }
                            });
                        }
                    });
                });
            },
            //强制刷新 跳转页面
            forceRefresh(cb) {
                this.getToken(true).then(bool => {
                    if (bool) {
                        let path = '';

                        let pages = getCurrentPages();

                        let nowPage = '/' + pages[pages.length - 1].route;

                        path = '/pages/index/index';

                        // console.log('999999999999',nowPage,path)

                        if (nowPage != path) {
                            uni.redirectTo({
                                url: path
                            });

                            cb && cb(true);

                            // this.$goNext(path,'redirectTo')
                        } else {
                            cb && cb();
                        }
                    } else {
                        cb && cb();
                    }
                });
            },
            //用户登录后赋值
            getCurrentUser(currentUser) {
                uni.setStorageSync('token', currentUser.token);

                this.globalData.userId = currentUser.userId;
                this.globalData.userInfo = currentUser.wechatDto;
                this.globalData.userMianInfo = currentUser.userInfo;
                this.globalData.avatarUrl = currentUser.wechatDto.headimgurl;

                this.globalData.villageInfo = currentUser.villageInfo || {};

                // SET_USER

                store.commit('SET_USER', currentUser);

                // this.$store.getters.selectProduct
            },
            //获取openid
            getOpenID(cb) {
                return this.globalData.wxCodeInfo.openId || this.globalData.userInfo.openid;
            },
            //
            //日志记录
            logRecord(remark) {
                return;

                var pages = getCurrentPages(); //获取加载的页面
                var currentPage = pages[pages.length - 1]; //获取当前页面的对象
                const options = currentPage.options; //获取当前页面的参数
                const route = currentPage.route; //获取当前页面的参数
                let userInfo = this.globalData.userInfo;

                let pageTit = undefined;

                try {
                    pageTit = __wxConfig.page[route + '.html'].window.navigationBarTitleText;
                } catch {}

                let isHasparams = Object.keys(options).length > 0;

                http({
                    url: 'access/log/saveorupdate',
                    data: {
                        describe: pageTit, //描述
                        accessPathParam: isHasparams ? options : undefined, //页面参数
                        accessPath: route, //访问路径
                        remark: remark, //备注
                        userId: userInfo.userId,
                        openId: this.getOpenID()
                    }
                }).then(res => {
                    // console.log('日志保存成功1111111')
                });
            }
        },
        onHide: function () {
            console.log('App Hide');
        }
    };
</script>
<style>
    @import url('./static/iconfont/iconfont.css');

    /* 适配底部安全区 */
        page {  
            --safe-area-inset-top: 0px;  
            --safe-area-inset-right: 0px;  
            --safe-area-inset-bottom: 0px;  
            --safe-area-inset-left: 0px;  

        }  

        @supports (top: constant(safe-area-inset-top)) {  
            page {  
                --safe-area-inset-top: constant(safe-area-inset-top);  
                --safe-area-inset-right: constant(safe-area-inset-right);  
                --safe-area-inset-bottom: constant(safe-area-inset-bottom);  
                --safe-area-inset-left: constant(safe-area-inset-left);  
            }  

        }  

        @supports (top: env(safe-area-inset-top)) {  
            page {  
                --safe-area-inset-top: env(safe-area-inset-top);  
                --safe-area-inset-right: env(safe-area-inset-right);  
                --safe-area-inset-bottom: env(safe-area-inset-bottom);  
                --safe-area-inset-left: env(safe-area-inset-left);  
            }  
        }  

    /*每个页面公共css */
    .allPage {
        position: relative;
        background: #f6f6f6;
        min-height: 100vh;
        box-sizing: border-box;
    }
    .w {
        padding: 0 32rpx;
        font-size: 28rpx;
        box-sizing: border-box;
    }
    .fix_user {
        position: fixed;
        bottom: 100px;
        z-index: 9999;
        right: 10px;
    }

    .fix_user .fix_user_img {
        width: 100rpx;
        height: 100rpx;
        border-radius: 1000px;
        display: block;
        border: 1px #fff solid;
    }

    .fix_user_text {
        border: 1rpx #eee solid;
        color: #444;
        font-size: 22rpx;
        border-radius: 1000rpx;
        padding: 5rpx 10rpx;
        background: #fff;
        position: absolute;
        bottom: -10rpx;
        left: 50%;
        transform: translateX(-50%);
        white-space: nowrap;
        line-height: 1;
    }

    .fix_user > button {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        opacity: 0;
    }
    page {
        font-family: PingFang SC;
        color: #000000;
        font-size: 24rpx;
    }
    button::after {
        border: none;
    }
    .container {
        min-height: 100vh;
        background-color: #f6f6f6;
        box-sizing: border-box;
        /* padding-top: 20rpx; */
    }
    /* 主要的主题色 start------------------------ */

    .main-color {
        color: #ef8948;
        color: #ff2f53;
    }
    .main-color-2 {
        color: #e89654;
        color: #ff2d46;
    }
    .main-color-3 {
        color: #f5e25d;
        /* color: rgb(253, 108, 26); */
    }
    .main-color-4 {
        color: #f6f9dc;
        color: rgb(246, 249, 220);
    }
    .price-col {
        color: #ff6e00 !important;
    }

    .main-color-5 {
        color: #f6f9dc;
        color: #ffe4e2;
    }

    .main-color-6 {
        color: #f6f9dc;
        color: #ffefee;
    }

    .main-color-7 {
        color: #f6f9dc;
        color: #ff5305;
    }

    /* 主题色 end--------------------------- */
    .col {
        color: #e7757d !important;
    }
    .col-red {
        color: #dd262d !important;
    }
    .col-blue {
        color: #0779fc !important;
    }
    .col-green {
        color: #00af63 !important;
    }
    .col-b {
        color: #000 !important;
    }
    .col-hui {
        color: #777 !important;
    }
    .col-3 {
        color: #333 !important;
    }
    .col-666 {
        color: #666 !important;
    }
    .col-999 {
        color: #999 !important;
    }
    .clear:after {
        content: ' ';
        display: block;
        height: 0;
        visibility: hidden;
        clear: both;
        font-size: 0;
    }

    .fl {
        float: left;
    }

    .fr {
        float: right;
    }
    .text-center {
        text-align: center !important;
    }
    .text-left {
        text-align: left !important;
    }
    .text-right {
        text-align: right !important;
    }
    .text-delic {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
    .uni-navbar--border {
        border-bottom: none !important;
    }
    .uni-nav-bar-text {
        font-size: 40rpx !important;
    }
    /* 统一公共的css */
    view {
        font-family: STHeiti SC, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, 'sans-serif';
    }
    .col {
        color: #ff5305;
    }
    .col-hui {
        color: #888;
    }
    .col-r {
        color: #ff0000 !important;
    }
    .col-h {
        color: #000000 !important;
    }
    .col-warn {
        color: #eaa319 !important;
    }
    .col-success {
        color: #1abc9c !important;
    }
    .col-m {
        color: #1587fa !important;
    }
    .back-fff {
        background-color: #ffffff !important;
    }
    .b {
        font-weight: 700 !important;
    }
    .clearfix:after {
        content: '';
        display: table;
        clear: both;
    }
    .n {
        font-family: 'PingFangSC-Regular,PingFangSC-Light,helvetica neue,hiragino sans gb,arial,microsoft yahei ui,microsoft yahei,simsun,sans-serif' !important;
        font-weight: 400;
    }
    .hide {
        display: none !important;
    }
    .over-hidden {
        overflow: hidden !important;
    }
    .text-delic {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .text-delic2 {
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .text-delic3 {
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
    }
    .f18 {
        font-size: 18rpx !important;
    }
    .f20 {
        font-size: 20rpx !important;
    }
    .f22 {
        font-size: 22rpx !important;
    }
    .f24 {
        font-size: 24rpx !important;
    }
    .f26 {
        font-size: 26rpx !important;
    }
    .f28 {
        font-size: 28rpx !important;
    }
    .f30 {
        font-size: 30rpx !important;
    }
    .f32 {
        font-size: 32rpx !important;
    }
    .f34 {
        font-size: 34rpx !important;
    }
    .f36 {
        font-size: 36rpx !important;
    }
    .margin0 {
        margin: 0 !important;
    }

    .mar_l16 {
        margin-left: 32rpx !important;
    }

    .mar_r16 {
        margin-right: 32rpx !important;
    }

    .ml5 {
        margin-left: 10rpx !important;
    }

    .mr5 {
        margin-right: 10rpx !important;
    }

    .mr10 {
        margin-right: 20rpx !important;
    }
    .mr20 {
        margin-right: 40rpx !important;
    }

    .ml10 {
        margin-left: 20rpx !important;
    }
    .ml15 {
        margin-left: 30rpx !important;
    }
    .ml20 {
        margin-left: 40rpx !important;
    }

    .mt5 {
        margin-top: 10rpx !important;
    }

    .mt10 {
        margin-top: 20rpx !important;
    }

    .mt15 {
        margin-top: 30rpx !important;
    }

    .mt20 {
        margin-top: 40rpx !important;
    }

    .mb5 {
        margin-bottom: 10rpx !important;
    }
    .mb8 {
        margin-bottom: 16rpx !important;
    }
    .mb10 {
        margin-bottom: 20rpx !important;
    }
    .mb15 {
        margin-bottom: 30rpx !important;
    }
    .mb20 {
        margin-bottom: 40rpx !important;
    }
    view {
        box-sizing: border-box;
    }
    .flex {
        display: flex;
    }
    .flex-1 {
        flex: 1;
    }
    .flex-center {
        align-items: center;
    }
    .parts {
        background: #fff;
        padding: 30rpx 40rpx 24rpx;
        border-radius: 6px;
        margin-bottom: 20rpx;
        font-size: 28rpx;
    }
    /* 底部按钮 */
    .commonFoot {
        height: 100px;
    }
    .commonFoot .foot-btn-box {
        width: 100%;
        height: 100px;
        padding-top: 30rpx;
    }
    .commonFoot .foot-btn-box.fixed {
        position: fixed;
        left: 0;
        bottom: 0;
        z-index: 10;
        background: #fff;
    }
    .commonFoot .foot-btn {
        margin: auto;
        width: 90%;
        border-radius: 30px;
        border: none;
        /* background-color: #ff7c8d; */
        /* background: linear-gradient(#ffa076, #ff7596); */
        background: linear-gradient(#ff5305, #ff2d46);

        color: #fff;
        height: 46px;
        font-size: 30rpx;
        line-height: 46px;
        text-align: center;
    }
    .commonFoot .foot-btn.green {
        background-color: #00af63 !important;
    }
    .commonFoot .foot-btn.warn {
        background-color: #eaa319 !important;
    }
    /* 列表没有数据 */
    .nodata-box {
        width: 100%;
        padding: 20rpx 0;
        margin-top: 50px;
        text-align: center;
    }
    .nodata-box .img {
        height: 280rpx;
        background: url('./static/images/nodata.png') center center no-repeat;
        background-size: contain;
    }
    .nodata-box text {
        margin-top: 40rpx;
        display: inline-block;
        font-size: 24rpx;
        color: #888;
    }
    .transBg {
        /* background: linear-gradient(#ffa076, #ff7596); */
        background: linear-gradient(#ff5305, #ff2d46);
    }
    .transBg2 {
        /* background: linear-gradient(#ff7596, #f3d9df,#fff); */
        background: linear-gradient(#ff2d46, rgb(255, 117, 150), #fff);
    }
    .transBg3 {
        /* background: linear-gradient(#ff7596, #f3d9df,#fff); */
        background: linear-gradient(#ffd1d1, #f6f6f6);
    }
    .transBg4 {
        background: linear-gradient(to bottom, #f7dce2 10%, #fff 70%);
    }
    .transBg5 {
        background: linear-gradient(111deg, #ffba44, #ff8f44) !important;
    }
    .transBg6 {
        /* background: linear-gradient(#ffa076, #ff7596); */
        background: linear-gradient(to right, red, #ff5305, #e62c42) !important;
    }
    /* //holderClass */
    .holderClass {
        color: rgba(0, 0, 0, 0.35);
    }

    .j-btn {
        margin: auto;
        width: 90%;
        border-radius: 30px;
        border: none;
        background: #eb4041;
        color: #fff;
        height: 48px;
        font-size: 34rpx;
        line-height: 46px;
        text-align: center;
    }
    /* 标签 */
    .j-tag {
        background-color: rgb(255, 248, 225);
        color: #e89654;
        line-height: 1.1;
        border-radius: 3rpx;
        font-size: 20rpx;
        padding: 4rpx 12rpx;
        margin: 4rpx 0 4rpx 8rpx;
        display: inline-block;
        vertical-align: top;
    }
    .j-tag.red {
        background-color: #f7dce2;
        color: #ff2d46;
    }
    .j-tag.red.full {
        background-color: #ff2d46;
        color: #f7dce2;
    }
    .j-tag.green.full {
        background-color: #00af63;
        color: #f7dce2;
    }

    .j-tag.green {
        background-color: #f0f9eb;
        border-color: #e1f3d8;
        color: #67c23a;
    }
    .j-tag.red {
        background-color: #fef0f0;
        border-color: #fde2e2;
        color: #ff0000;
    }
    .j-tag.hui {
        background-color: #f1f1f1;
        border-color: transparent;
        color: #999;
    }
    .j-tag.warn {
        background-color: #f9ebe1;
        border-color: transparent;
        color: #ff6e00;
    }

    .j-tag.full {
        background-color: #1890ff;
        border-color: #1890ff;
        color: #fff;
    }

    .j-tag.green.full {
        background-color: #67c23a;
        border-color: #67c23a;
        color: #fff;
    }
    .j-tag.red.full {
        background-color: #ff0000;
        border-color: #ff0000;
        color: #fff;
    }
    .j-tag.warn.full {
        background-color: #ff6e00;
        border-color: #ff6e00;
        color: #fff;
    }

    .j-tag.hui.full {
        background-color: #999;
        border-color: #999;
        color: #fff;
    }
</style>
